package com.example.medical.controller.admin;

import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.dto.AdminSaveDivisionDto;
import com.example.medical.entity.dto.AdminUpdateDivisionDto;
import com.example.medical.service.DivisionService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/division")
public class AdminDivisionController {

    @Autowired
    private DivisionService divisionService;

    @PostMapping("/get_division_list")
    public Result AdminGetDivisionList(@RequestBody GetListDto dto){
        return divisionService.AdminGetDivisionList(dto);
    }

    @GetMapping("/get_division_detail")
    public Result AdminGetDivisionDetail(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(divisionService.getById(id));
    }

    @GetMapping("/update_get_division")
    public Result AdminUpdateGetDivision(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(divisionService.getById(id));
    }

    @PostMapping("/update_division")
    public Result AdminUpdateDivision(@RequestBody AdminUpdateDivisionDto dto){
        return divisionService.AdminUpdateDivision(dto);
    }

    @PostMapping("/save_division")
    public Result AdminSaveDivision(@RequestBody AdminSaveDivisionDto dto){
        return divisionService.AdminSaveDivision(dto);
    }

    @GetMapping("/remove_division")
    public Result AdminRemoveDivision(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        boolean checkRemove = divisionService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_division_arr")
    public Result AdminRemoveDivisionArr(@RequestParam("ids")String[] ids){
        return divisionService.AdminRemoveDivisionArr(ids);
    }

}
